package tw.chen.unlock;

import android.app.Activity;
import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;

public class WidgetConfigure extends Activity 
{
	private String TAG = this.getClass().getName();
	private boolean ENABLE = false;
	int appWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
	int NEW_AXIS;
	int AXIS = 0;
	int DIRECT = 0;
	int NEW_DIRECT;
	int DEGREES = 30;
	int NEW_DEGREES;
	int DIRFT = 10;
	int NEW_DRIFT;
	
	private Button okay, test;
	private Spinner spinner;
	private CheckBox check;
	private RadioGroup group;
	private EditText angle, drift;
	private RadioButton axis_x, axis_y, axis_z;
	
	Context context;
	
	SharedPreferences setting;

	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.widget_configure);
		
		context = this;
		setting = getSharedPreferences(Values.SETTING, Values.SETTING_PAGE);
		
		okay = (Button)findViewById(R.id.button_okay);
		test = (Button)findViewById(R.id.button_test);
		check = (CheckBox)findViewById(R.id.check);
		group = (RadioGroup)findViewById(R.id.group);
		axis_x = (RadioButton)findViewById(R.id.radio_x);
		axis_y = (RadioButton)findViewById(R.id.radio_y);
		axis_z = (RadioButton)findViewById(R.id.radio_z);
		angle = (EditText)findViewById(R.id.edit_angle);
		drift = (EditText)findViewById(R.id.edit_drift);
		spinner = (Spinner)findViewById(R.id.spinner);
		
		spinner.setAdapter(getAdapter());

		okay.setOnClickListener(click);
		test.setOnClickListener(click);
		check.setOnCheckedChangeListener(listener);
		group.setOnCheckedChangeListener(change);
		spinner.setOnItemSelectedListener(selector);
		
		init();
		setValue();
	}
	
	private void init()
	{
		setResult(RESULT_CANCELED);
		
		Bundle extras = getIntent().getExtras();
		
		if (extras != null)
		{
			appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID
					, AppWidgetManager.INVALID_APPWIDGET_ID);
		}
		if (appWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID)
			finish();
	}
	
	private ArrayAdapter<CharSequence> getAdapter()
	{
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource
				(context, R.array.direction, android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		return adapter;
	}
	
	private void setValue()
	{
		ENABLE = setting.getBoolean(Values.STATUS, false);
		if (ENABLE)
			check.setChecked(true);
		else
			check.setChecked(false);
		
		AXIS = setting.getInt(Values.AXIS_TYPE, Values.AXIS_Y);
		switch (AXIS)
		{
		case Values.AXIS_X:
			axis_x.setChecked(true);
			break;
		case Values.AXIS_Y:
			axis_y.setChecked(true);
			break;
		case Values.AXIS_Z:
			axis_z.setChecked(true);
			break;
		}
		
		DEGREES = setting.getInt(Values.DEGREES, 30);
		angle.setText(String.valueOf(DEGREES));
		
		DIRECT = setting.getInt(Values.DIRECTION, 0);
		spinner.setSelection(DIRECT);
		
		DIRFT = setting.getInt(Values.DRIFT, 10);
		drift.setText(Integer.toString(DIRFT));
	}
		
	private OnClickListener click = new OnClickListener()
	{
		@Override
		public void onClick(View view) 
		{
			switch (view.getId())
			{
			case R.id.button_okay:

				Intent broadcast = new Intent(Values.START_SERVICE);
				sendBroadcast(broadcast);

				AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
				WidgetProvider.updateAppWidget(context, appWidgetManager, appWidgetId, ENABLE);
				
				Intent result = new Intent();
				result.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
				setResult(RESULT_OK, result);
				finish();
				break;
			case R.id.button_test:
				Intent testPage = new Intent();
				testPage.setClass(context, TestActivity.class);
				startActivity(testPage);
				break;
			}
			NEW_DEGREES = Integer.valueOf(angle.getText().toString());
			NEW_DRIFT = Integer.valueOf(drift.getText().toString());
			
			setting.edit().putBoolean(Values.STATUS, ENABLE).commit();
			setting.edit().putInt(Values.AXIS_TYPE, NEW_AXIS).commit();
			setting.edit().putInt(Values.DIRECTION, NEW_DIRECT).commit();
			setting.edit().putInt(Values.DEGREES, NEW_DEGREES).commit();
			setting.edit().putInt(Values.DRIFT, NEW_DRIFT).commit();
		}
	};
	
	private OnItemSelectedListener selector = new OnItemSelectedListener()
	{

		@Override
		public void onItemSelected(AdapterView<?> arg0, View arg1, int pos, long arg3) 
		{
			NEW_DIRECT = pos;
		}

		@Override
		public void onNothingSelected(AdapterView<?> arg0) 
		{
			NEW_DIRECT = DIRECT;
		}
	};
	
	private CompoundButton.OnCheckedChangeListener listener = new CompoundButton.OnCheckedChangeListener()
	{
		@Override
		public void onCheckedChanged(CompoundButton arg0, boolean isChecked) 
		{
			if (isChecked)
				ENABLE = true;
			else
				ENABLE = false;
		}
	};
	
	private RadioGroup.OnCheckedChangeListener change = new RadioGroup.OnCheckedChangeListener()
	{
		@Override
		public void onCheckedChanged(RadioGroup group, int checkedId) 
		{
			switch (checkedId)
			{
			case R.id.radio_x:
				NEW_AXIS = Values.AXIS_X;
				break;
			case R.id.radio_y:
				NEW_AXIS = Values.AXIS_Y;
				break;
			case R.id.radio_z:
				NEW_AXIS = Values.AXIS_Z;
				break;
			}
			Log.i(TAG, "new axis = " + NEW_AXIS);
		}
	};
}
